package com.asset.salesforce.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asset.salesforce.domain.City;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.Lead;
import com.asset.salesforce.domain.LogicalOperators;
import com.asset.salesforce.domain.View;
import com.asset.salesforce.domain.ViewFields;
import com.asset.salesforce.domain.ViewQuery;
import com.asset.salesforce.domain.ViewSelectedFields;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.JoinRequestService;
import com.asset.salesforce.service.LeadService;
import com.asset.salesforce.service.LogicalOperatorsService;
import com.asset.salesforce.service.ViewFieldsService;
import com.asset.salesforce.service.ViewQueryService;
import com.asset.salesforce.service.ViewSelectedFieldsService;
import com.asset.salesforce.service.ViewService;

@Controller
public class ViewsController {

	@Value("${Lead}")
	private String Lead;
	@Value("${Account}")
	private String Account;
	@Value("${Contact}")
	private String Contact;
	@Value("${Call}")
	private String Call;
	@Value("${Appointment}")
	private String Appointment;
	
	public String getLead() {
		return Lead;
	}
	public void setLead(String lead) {
		Lead = lead;
	}
	public String getAccount() {
		return Account;
	}
	public void setAccount(String account) {
		Account = account;
	}
	public String getContact() {
		return Contact;
	}
	public void setContact(String contact) {
		Contact = contact;
	}
	public String getCall() {
		return Call;
	}
	public void setCall(String call) {
		Call = call;
	}
	public String getAppointment() {
		return Appointment;
	}
	public void setAppointment(String appointment) {
		Appointment = appointment;
	}

	@Autowired
	private ViewFieldsService viewFieldsService;
	
	@Autowired
	private LogicalOperatorsService logicalOperatorsService;

	@Autowired
	private ViewQueryService viewQueryService;
	
	@Autowired
	private ViewService viewService;
	
	@Autowired
	private ViewSelectedFieldsService viewSelectedFieldsService;

	@Autowired
	private LeadService leadService;
	
	@Autowired
	private JoinRequestService joinRequestService;

	@Autowired
	private CalendarService calendarService;
	
	@RequestMapping(value="/new-lead-view",method=RequestMethod.GET)
	public String getNewLeadView(Model model,HttpSession session){
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/login.html";
		}
		
			List<LogicalOperators> logicalOperators = logicalOperatorsService.getAll();
			model.addAttribute("logicalOperators",logicalOperators);
			
			String viewField = getLead();
			String[] viewFields = viewField.split(",");
			model.addAttribute("viewFields",viewFields);
			
			Employee userDetails = (Employee)session.getAttribute("userDetails");
			model.addAttribute("userDetails",userDetails);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			return "new-lead-view";
		
	}
	@RequestMapping(value="/new-lead-view" ,method=RequestMethod.POST)
	public String postNewLeadView(HttpSession session,
			@RequestParam(value="viewName",required =false) String viewName,
			@RequestParam(value="uniqueName",required =false) String uniqueName,
			@RequestParam(value="fieldLogic1",required =false) String fieldLogic1,
			@RequestParam(value="operator1",required =false) Integer operator1,
			@RequestParam(value="value1",required =false) String value1,
			@RequestParam(value="fieldLogic2",required =false) String fieldLogic2,
			@RequestParam(value="operator2",required =false) Integer operator2,
			@RequestParam(value="value2",required =false) String value2,
			@RequestParam(value="fieldLogic3",required =false) String fieldLogic3,
			@RequestParam(value="operator3",required =false) Integer operator3,
			@RequestParam(value="value3",required =false) String value3,
			@RequestParam(value="fieldsToDisplay",required =false) String fieldsToDisplay,
			@RequestParam(value="visibility",required =false) String visibility,
			@RequestParam(value="viewTypeId",required =false) Integer viewTypeId,
			@RequestParam(value="reportPeriod", required=false) String reportPeriod,
			@RequestParam(value="fromDomain", required=false) String fromDomain){
		
		
		Employee viewOwner = (Employee)session.getAttribute("userDetails");
		View view = new View();
		
		view.setName(viewName);
		view.setUniqueName(uniqueName);
		view.setVisible(true);
		view.setViewOwner(viewOwner);
		view.setPeriodType(reportPeriod);
		view.setFromDomain(fromDomain);
		
		viewService.addView(view);
		
		LogicalOperators logicalOperator1 = logicalOperatorsService.getLogicalOperatorById(operator1);
		LogicalOperators logicalOperator2 = logicalOperatorsService.getLogicalOperatorById(operator2);
		LogicalOperators logicalOperator3 = logicalOperatorsService.getLogicalOperatorById(operator3);

		System.out.println("fields to display... " + fieldsToDisplay);
		String[] fields = fieldsToDisplay.split("&");
		for (int i = 0; i < fields.length; i++) {
			System.out.println("field idddddddddddddddd............ " + fields[i]);
			//Integer fieldId = Integer.parseInt(fields[i]);
			//ViewFields viewField = viewFieldsService.getViewFieldById(fieldId);
			ViewSelectedFields viewSelectedFields = new ViewSelectedFields();
			viewSelectedFields.setFieldName(fields[i]);
			viewSelectedFields.setView(view);
			viewSelectedFields.setFieldOrder(i);
			viewSelectedFieldsService.addViewSelectedField(viewSelectedFields);
		}
		
		ViewQuery viewQuery1 = new ViewQuery();
		ViewQuery viewQuery2 = new ViewQuery();
		ViewQuery viewQuery3 = new ViewQuery();
		
		viewQuery1.setViewId(view);
		viewQuery1.setFieldName(fieldLogic1);
		viewQuery1.setQueryOperatorId(logicalOperator1);
		viewQuery1.setQueryValue(value1);
		viewQueryService.addViewQuery(viewQuery1);
		
		viewQuery2.setViewId(view);
		viewQuery2.setFieldName(fieldLogic2);
		viewQuery2.setQueryOperatorId(logicalOperator2);
		viewQuery2.setQueryValue(value2);
		viewQueryService.addViewQuery(viewQuery2);
		
		viewQuery3.setViewId(view);
		viewQuery3.setFieldName(fieldLogic3);
		viewQuery3.setQueryOperatorId(logicalOperator3);
		viewQuery3.setQueryValue(value3);
		viewQueryService.addViewQuery(viewQuery3);
		
		return "redirect:/s/views.html";	
	}
	@RequestMapping(value="/view",method=RequestMethod.GET)
	public String getView(Model model,HttpSession session,
			@RequestParam(value="viewId",required =false) Integer viewId,
			@RequestParam(value="startDate",required =false) String startDate,
			@RequestParam(value="endDate",required =false) String endDate){
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/login.html";
		}
			Employee leadViewOwner = (Employee)session.getAttribute("userDetails");
			View view = viewService.getViewById(viewId);
			List<ViewSelectedFields> viewSelectedFields = viewSelectedFieldsService.getViewSelesctedFields(viewId);
			List<String> viewFields = new ArrayList<String>();
			List<String> fields = new ArrayList<String>();
			for (ViewSelectedFields viewSelectedField : viewSelectedFields) {
				fields.add(viewSelectedField.getFieldName());
			}
			System.out.println("gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg" + viewSelectedFields.size());
			System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + viewFields.size());
			List<ViewQuery> viewQueries = viewQueryService.getViewQueries(viewId);
			System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + viewQueries.size());
			List<String> cols = new ArrayList<String>();
			List<String> operators = new ArrayList<String>();
			List<String> values = new ArrayList<String>();

			for (ViewQuery viewQuery : viewQueries) {

				cols.add(viewQuery.getFieldName());	
				operators.add( viewQuery.getQueryOperatorId().getOperator());	
				values.add(viewQuery.getQueryValue());
			}
			String fromDomain = view.getFromDomain();
			System.out.println("################################## my from domain: " + fromDomain);
			List<String> viewLeads;
			if(startDate != null && !startDate.equalsIgnoreCase("")){
				viewLeads = leadService.getViewCustomDate(fields, cols, operators, values, fromDomain, startDate, endDate);
			}
			else{
				String reportPeriod = view.getPeriodType();
				viewLeads = leadService.getViewLeads(fields, cols, operators, values, fromDomain, reportPeriod);
			}

			System.out.println("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk" + viewLeads.size());
			model.addAttribute("viewLeads",viewLeads);
			model.addAttribute("view",view);
			if(fields.size() > 0){
				model.addAttribute("fields",fields);
			}
			model.addAttribute("userDetails",leadViewOwner);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);

			return "view";
	}

	@RequestMapping(value="/views",method=RequestMethod.GET)
	public String getViews(Model model,HttpSession session,
			@RequestParam(value="viewId",required =false) Integer viewId){
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/login.html";
		}
			Employee leadViewOwner = (Employee)session.getAttribute("userDetails");
			List<View> empViews = viewService.getViews(leadViewOwner.getId());
			model.addAttribute("empViews",empViews);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			return "views";
	}

	@RequestMapping(value = "/getViewDomainCols", method = RequestMethod.POST)
	public @ResponseBody
	String getViewDomainCols(Model model, HttpSession session, HttpServletRequest request) {
		String selectedDomain = request.getParameter("selectedDomain");
		System.out.println("!!!!!!!!!!!!!!!!!!!!!!!! getViewDomainCols .................... " + selectedDomain);
		String viewField = "EMPTY";
		
		if (selectedDomain.equalsIgnoreCase("Lead")) {
			viewField = getLead();
		} else if (selectedDomain.equalsIgnoreCase("Account")) {
			viewField = getAccount();
		} else if (selectedDomain.equalsIgnoreCase("Contact")) {
			viewField = getContact();
		} else if (selectedDomain.equalsIgnoreCase("Call")) {
			viewField = getCall();
		} else if (selectedDomain.equalsIgnoreCase("Appointment")) {
			viewField = getAppointment();
		}
		System.out.println("!!!!!!!!!!!!!!!!!!!!!!!! viewField .................... " + viewField);
		return viewField;
	}
	
}
